/**
 * Created by meiguizhinian on 2018/7/10
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//TODO：等待完成
//cocos2dx class
LLTeaching = cc.LayerColor.extend({
	ctor: function () {
		this._super(cc.color(127,127,127, 0));

		let clip = new cc.ClippingNode();//cocos2d::ClippingNode::create();
		clip.setInverted(true);//设置底板可见
		clip.setAlphaThreshold(0);//设置透明度Alpha值为0
		this.addChild(clip);

		// 裁剪节点-底板
		let back= new cc.LayerColor(cc.color(0,0,0,0));
		clip.addChild(back);//为裁剪节点添加一个黑色带透明（看起了是灰色）的底板
		// 裁剪节点-模版
		this._stencil= new cc.DrawNode();//cocos2d::DrawNode::create();
		clip.setStencil(this._stencil);
		// 目标边框
		this._border = new cc.DrawNode();
		this.addChild(this._border);
		// 指导箭头
		this._arrow = new cc.Sprite("vfs/arrow.png");
		this.addChild(this._arrow);

		//屏蔽触摸，仅模版范围内的触摸有效
		let listener = {
			event : cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches : true,
		};
		listener.onTouchBegan = function (touch, event) {
			cc.log("触摸屏蔽");
			return true;
		};
		listener.onTouchBegan = function(t, e) {
			let node = e.getCurrentTarget();
			let location = node.getParent().convertToNodeSpace(t.getLocation());
			if (cc.rectContainsPoint(node.getBoundingBox(), location)) {
				if (this._stencil) {//只有点击在模版内的不被吞噬
					let location = t.getLocation();
					let size = this._stencil.getContentSize();
					let pos = this._stencil.getPosition();
					if (cc.rectContainsPoint(cc.rect(pos.x, pos.y, size.width, size.height), location)) {
						return false;
					}
				}
				return true;//其他的点击全部屏蔽
			}
			return false;
		}.bind(this);
		listener.onTouchEnded = function() {
			if (!this._force) {
				//非强制情况下，点击在模板外取消教学
				this.removeFromParent();
			}
		}.bind(this);
		cc.eventManager.addListener(cc.EventListener.create(listener), this);
	},
	/*
	 * 设置模版矩形
	 * @param rect {cc.rect} 起始矩形
	 * @param vector {cc.Point} 移动向量
	 *  */
	demos : function(rect, vector) {
		let size = cc.size(rect.width, rect.height);
		size.width += Math.abs(vector.x);
		size.height += Math.abs(vector.y);
		let pos = cc.p(rect.x, rect.y);

		if (vector.x<0 || vector.y<0) {
			pos = cc.pAdd(pos, vector);
		}
		this._viewFrame = cc.rect(pos.x, pos.y, size.width, size.height);
		// 模板
		this._stencil.setContentSize(size);
		this._stencil.clear();
		//this._stencil.drawSolidRect(cc.p(0,0), cc.p(size.width,size.height), cc.color(255, 255, 255, 255));
		this._stencil.setPosition(pos);
		// 目标位置边框
		pos = cc.pAdd(rect, vector);
		this._border.setPosition(pos);
		this._border.clear();
		this._border.drawRect(cc.p(0,0), cc.p(rect.width, rect.height), cc.color(255, 255, 255, 0), !cc.sys.isNative+1);
		this._endFrame = cc.rect(pos.x, pos.y, rect.width, rect.height);
		// 箭头
		pos.x = rect.x + rect.width/2;
		pos.y = rect.y + rect.height/2;
		this._arrow.setPosition(pos);
		let angle = cc.radiansToDegrees(cc.pToAngle(vector));
		this._arrow.setRotation(-angle);
		// 箭头摆动动作
		this._arrow.stopAllActions();
		let move = cc.moveBy(1, vector);
		let action = cc.sequence(move, move.reverse());
		this._arrow.runAction(cc.repeatForever(action));
	},
	getViewFrame : function() {
		return this._viewFrame;
	},
	getEndFrame : function() {
		return this._endFrame;
	},

	//是否强制只能在模板举行内的点击有效
	setForce : function(force) {this._force = force;},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private:
	_force : false,
	_viewFrame : null,  // 教学显示区域 cppsupport::Rect<2, float>
	_endFrame : null,  // 结束位置 cppsupport::Rect<2, float>

	_stencil : null,  // 裁剪模版 cocos2d::DrawNode*
	_border : null,  // 目标位置边框 cocos2d::DrawNode*
	_arrow : null,  // 箭头 cocos2d::Sprite*
});

LLTeaching.create = function () {
	return new LLTeaching();
};
